package com.csdemos.algs.table;

import com.csdemos.algs.function.ResearchFunction;

public class OpenAddressHashTable implements HashTable{

    protected ResearchFunction researchFunction;
    
    protected Integer[] table;
    protected boolean[] free;
    protected final int size;
    
    public OpenAddressHashTable(ResearchFunction func) {
        researchFunction = func;
        size = ResearchFunction.m;
        table = (Integer[])new Integer[size];
        free = new boolean[size];
        for(int i = 0; i < size; i++){
            free[i] = true;
        }
        
    }
    
    @Override
    public Integer insert(int value) throws IllegalArgumentException{
        int i = 0;
        int j;
        while(i < size){
            j = researchFunction.next(value, i);
            if(free[j]){
                table[j] = value;
                return j;
            } else {
            	i++;
            }
        }
        throw new IllegalArgumentException("Hashtable is full.");
    }

    @Override
    public Integer search(int key) throws IllegalArgumentException{
        int i = 0;
        int j;
        while(i < 0){
            j = researchFunction.next(key, i);
            if(table[j] == key)
                return j;
            if(table[j] == null)
                throw new IllegalArgumentException();
            i++;
        }
        throw new IllegalArgumentException();
    }
    
    @Override
    public boolean delete(int value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
